package com.lezhi.socialgram.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class PrimaryGenerater {
 
//    private static final String SERIAL_NUMBER = "XXXX"; // 流水号格式
    private static PrimaryGenerater primaryGenerater = null;
    private String curSerialNo = null;
 
    private PrimaryGenerater() {
    }
 
    /**
     * 取得PrimaryGenerater的单例实现
     *
     * @return
     */
    public static PrimaryGenerater getInstance() {
        if (primaryGenerater == null) {
            synchronized (PrimaryGenerater.class) {
                if (primaryGenerater == null) {
                    primaryGenerater = new PrimaryGenerater();
                }
            }
        }
        return primaryGenerater;
    }
 
    /**
     * 生成下一个订单编号
     */
    public synchronized String generaterNextOrderNumber() {
        String id = null;
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String datePrefix = formatter.format(date).substring(2);
        if (curSerialNo == null) {
            id = datePrefix + "001";
        } else {
            DecimalFormat df = new DecimalFormat("000");
            id = datePrefix
                    + df.format(1 + Integer.parseInt(curSerialNo.substring(12,15))%1000);
            curSerialNo = id;
        }
        return id;
    }
    
    /**
     * 生成下一个外部编号
     */
    public synchronized String generaterNextExternalTradeNumber() {
        String id = null;
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String datePrefix = formatter.format(date).substring(2);
        if (curSerialNo == null) {
            id = datePrefix + "001";
        } else {
            DecimalFormat df = new DecimalFormat("000");
            id = datePrefix
                    + df.format(1 + Integer.parseInt(curSerialNo.substring(12, 15))%1000);
            curSerialNo = id;
        }
        return "E"+id;
    }
}